{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "93cd331e-03a0-46e0-8862-40fbe4e33189",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-05T19:19:01.003083Z",
     "iopub.status.busy": "2022-02-05T19:19:01.002511Z",
     "iopub.status.idle": "2022-02-05T19:19:05.453755Z",
     "shell.execute_reply": "2022-02-05T19:19:05.453074Z",
     "shell.execute_reply.started": "2022-02-05T19:19:01.002974Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62174644-b79a-46c2-9823-cb309eec889d",
   "metadata": {},
   "source": [
    "1. 布尔索引通过相同数组元素位置上的布尔值来提取元素\n",
    "2. 逻辑条件可以是多个，有逻辑与(&)和逻辑或(|)连接\n",
    "3. 多条件中，每个条件要使用小括号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4e598731-bece-48c2-b345-fc8f3579e248",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-05T19:21:15.715610Z",
     "iopub.status.busy": "2022-02-05T19:21:15.715363Z",
     "iopub.status.idle": "2022-02-05T19:21:15.721465Z",
     "shell.execute_reply": "2022-02-05T19:21:15.720182Z",
     "shell.execute_reply.started": "2022-02-05T19:21:15.715584Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]\n",
      " [15 16 17 18 19]]\n"
     ]
    }
   ],
   "source": [
    "a1 = np.arange(20).reshape((4, 5))  # 创建元素为0到19的整数数组，并设置shape为4行5列\n",
    "print(a1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f0aacd27-d10f-450f-8dcd-2e61e2558d4f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-05T19:21:41.303122Z",
     "iopub.status.busy": "2022-02-05T19:21:41.302681Z",
     "iopub.status.idle": "2022-02-05T19:21:41.307447Z",
     "shell.execute_reply": "2022-02-05T19:21:41.306736Z",
     "shell.execute_reply.started": "2022-02-05T19:21:41.303095Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ True  True  True  True  True]\n",
      " [ True  True  True  True  True]\n",
      " [False False False False False]\n",
      " [False False False False False]]\n"
     ]
    }
   ],
   "source": [
    "print(a1 < 10)  # 对数组a1做逻辑运算，得到布尔索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2a3b358b-f364-4d53-aa3b-1540ffbe72d9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-05T19:21:56.351304Z",
     "iopub.status.busy": "2022-02-05T19:21:56.351012Z",
     "iopub.status.idle": "2022-02-05T19:21:56.355502Z",
     "shell.execute_reply": "2022-02-05T19:21:56.354733Z",
     "shell.execute_reply.started": "2022-02-05T19:21:56.351275Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "print(a1[a1 < 10])  # 用布尔索引从数组中取得对应的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "11037a1f-1487-4ba4-888a-6a30768e1a92",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-05T20:22:55.391976Z",
     "iopub.status.busy": "2022-02-05T20:22:55.391512Z",
     "iopub.status.idle": "2022-02-05T20:22:55.396518Z",
     "shell.execute_reply": "2022-02-05T20:22:55.395498Z",
     "shell.execute_reply.started": "2022-02-05T20:22:55.391950Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4 11 12 13 14 15 16 17 18 19]\n"
     ]
    }
   ],
   "source": [
    "print(a1[(a1 < 5) | (a1 > 10)]) # 两个条件的例子"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
